program drop _all
program define FIELLERprog, rclass
version 14
syntax, beta0(real) betay(real) betax(real) smallobs(integer) bigobs(integer)
// Remove existing variables
drop _all


// Create the data
set obs `bigobs' 
gen t = _n
tsset t
// We initialize y as its LR equilibrium value
gen x=rnormal()
gen u=rnormal()
gen y = `beta0'/(1-`betay')
replace y = `beta0' + `betax'*x+`betay'*L.y + u in 2/l


// Wald test
regress y L.y x in -`smallobs'/l
testnl _b[x]/(1-_b[L.y])= `betax'/(1-`betay')
return scalar pWald = r(p)

// Fieller's test
mat cov = e(V)
matrix VV = cov[1..2, 1..2]
scalar LRP0 = `betax'/(1-`betay')
scalar ztest = (_b[x]- LRP0*(1-_b[L.y]))/sqrt(LRP0^2*VV[1,1]+2*LRP0*VV[1,2]+VV[2,2])
return scalar pFieller = (1 - normal(abs(ztest)))*2


end
